package com.hellohoper.script.config.admin.app;

import com.hellohoper.cloud.starter.redis.annotation.EnableRedis;
import com.hellohoper.cloud.starter.swagger.annotation.EnableSwaggerApi;
import com.hellohoper.cloud.starter.zookeeper.annotation.EnableZookeeper;
import com.hellohoper.script.config.admin.app.dao.ScriptConfigMapper;
import com.hellohoper.script.config.engine.container.ScriptContainer;
import com.hellohoper.script.config.engine.run.ScriptRunnerService;
import com.hellohoper.script.config.engine.run.bean.RunnerParam;
import com.hellohoper.script.config.engine.run.bean.RunnerResult;
import com.hellohoper.script.config.starter.EnableGroovyConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;

/**
 * 主程序
 * Created by hk on 12/14/16.
 */
@SpringBootApplication
@EnableEurekaClient
@EnableGroovyConfig
@EnableSwaggerApi
@EnableRedis
@EnableTransactionManagement
@MapperScan(basePackages = "com.hellohoper.script.config.admin.app.dao")
public class ScriptConfigAdminApp implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptConfigAdminApp.class);

    @Autowired
    private ScriptRunnerService<String, String> scriptRunnerService;

    public static void main(String[] args) {
        SpringApplication.run(ScriptConfigAdminApp.class, args);
    }

    @Override
    public void run(String... strings) throws Exception {
        LOGGER.info("程序启动。。。。。。。");
        RunnerParam runnerParam = new RunnerParam();

        long start = 0L;
        for (int i = 0; i < 10000; i++) {
            RunnerResult<String, String> run = scriptRunnerService.run("a", runnerParam);
            if (i == 0) {
                start = System.currentTimeMillis();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);

    }

    @Bean
    public AlwaysSampler defaultSampler(){
        // 分布式追踪，默认追踪全部
        return new AlwaysSampler();
    }

}
